home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / misc / pdflib / text2pdf.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  4KB  |  176 lines

  1. /* text2pdf.c
  2.  * Copyright (C) 1997-98 Thomas Merz. All rights reserved.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. #ifdef POSIX
  10. #include <unistd.h>
  11. #endif
  12.  
  13. #ifdef DOS
  14. #include <process.h>
  15. #endif
  16.  
  17. #ifdef NeXT
  18. #include <libc.h>    /* for getopt(), optind, optarg */
  19. #endif
  20.  
  21. #include "pdf.h"
  22.  
  23. static void
  24. usage(void)
  25. {
  26.     fprintf(stderr, "text2pdf - convert text files to pdf. (C) Thomas Merz 1997-98\n");
  27.     fprintf(stderr, "usage: text2pdf [options] [textfile]\n");
  28.     fprintf(stderr, "Available options:\n");
  29.     fprintf(stderr, "-b            binary mode (default: ASCII)\n");
  30.     fprintf(stderr, "-f fontname   name of font to use\n");
  31.     fprintf(stderr, "-h height     page height in points\n");
  32.     fprintf(stderr, "-m margin     margin size in points\n");
  33.     fprintf(stderr, "-s size       font size\n");
  34.     fprintf(stderr, "-o filename   PDF output file name\n");
  35.     fprintf(stderr, "-w width      page width in points\n");
  36.     fprintf(stderr, "-I path       path to AFM and font directory\n");
  37.  
  38.     exit(1);
  39. }
  40.  
  41. #define BUFLEN 512
  42.  
  43. void
  44. main(int argc, char *argv[])
  45. {
  46.     PDF_info    *info;
  47.     char    buf[BUFLEN], *s;
  48.     FILE    *pdffile = NULL, *textfile = stdin;
  49.     PDF        *p;
  50.     int        opt;
  51.     char    *fontname;
  52.     float    fontsize;
  53.     float    x, y, width = a4.width, height = a4.height, margin = 20;
  54.     
  55.     info        = PDF_get_info();
  56.     info->Title        = "stdin";
  57.     info->Creator       = "text2pdf";
  58.     info->binary_mode    = false;
  59.  
  60.     fontname        = "Courier";
  61.     fontsize        = 12.0;
  62.  
  63.     while ((opt = getopt(argc, argv, "bf:h:m:o:s:w:I:")) != -1)
  64.     switch (opt) {
  65.         case 'b':
  66.         info->binary_mode = true;
  67.         break;
  68.         
  69.         case 'f':
  70.         fontname = optarg;
  71.         break;
  72.  
  73.         case 'h':
  74.         height = atoi(optarg);
  75.         if (height < 0) {
  76.             fprintf(stderr, "Error: bad page height %f!\n", height);
  77.             usage();
  78.         }
  79.         break;
  80.  
  81.         case 'm':
  82.         margin = atoi(optarg);
  83.         if (margin < 0) {
  84.             fprintf(stderr, "Error: bad margin %f!\n", margin);
  85.             usage();
  86.         }
  87.         break;
  88.  
  89.         case 'o':
  90.         pdffile = fopen(optarg, WRITEMODE);
  91.         if (pdffile == NULL) {
  92.             fprintf(stderr, 
  93.                 "Error: cannot open output file %s.\n", optarg);
  94.             usage();
  95.         }
  96.         break;
  97.  
  98.         case 's':
  99.         fontsize = atoi(optarg);
  100.         if (fontsize < 0) {
  101.             fprintf(stderr, "Error: bad font size %f!\n", fontsize);
  102.             usage();
  103.         }
  104.         break;
  105.  
  106.         case 'w':
  107.         width = atoi(optarg);
  108.         if (width < 0) {
  109.             fprintf(stderr, "Error: bad page width %f!\n", width);
  110.             usage();
  111.         }
  112.         break;
  113.  
  114.         case 'I':
  115.         info->fontpath = optarg;
  116.         break;
  117.  
  118.         case '?':
  119.         default:
  120.         usage();
  121.     }
  122.  
  123.     if (pdffile == (FILE *) NULL)
  124.     usage();
  125.  
  126.     if (optind < argc) {
  127.     info->Title    = argv[optind];
  128.     if ((textfile = fopen(argv[optind], "r")) == NULL) {
  129.         fprintf(stderr, "Error: cannot open input file %s.\n",argv[optind]);
  130.         exit(2);
  131.     }
  132.     } else
  133.     textfile = stdin;
  134.  
  135.     p = PDF_open(pdffile, info);
  136.  
  137.     x = margin;
  138.     y = height - margin;
  139.  
  140.     while ((s = fgets(buf, BUFLEN, textfile)) != NULL) {
  141.     if (s[0] == '\f') {
  142.         if (y == height - margin)
  143.         PDF_begin_page(p, width, height);
  144.         PDF_end_page(p);
  145.         y = height - margin;
  146.         continue;
  147.     }
  148.  
  149.     if (s[0] != '\0' && s[strlen(s) - 1] == '\n')
  150.         s[strlen(s) - 1] = '\0';    /* remove newline character */
  151.  
  152.     if (y < margin) {        /* page break necessary? */
  153.         y = height - margin;
  154.         PDF_end_page(p);
  155.     }
  156.  
  157.     if (y == height - margin) {
  158.         PDF_begin_page(p, width, height);
  159.         PDF_set_font(p, fontname, fontsize, winansi);
  160.         PDF_set_text_pos(p, x, y);
  161.         y -= fontsize;
  162.     }
  163.  
  164.     PDF_continue_text(p, s);
  165.     y -= fontsize;
  166.  
  167.     }
  168.  
  169.     if (y != height - margin)
  170.     PDF_end_page(p);
  171.  
  172.     PDF_close(p);
  173.  
  174.     exit(0);
  175. }
  176.